<?php
/**
 * 登陆
 */
class Apiv1_LoginController extends Ctrl_Api{
	public function indexAction(){
		$p = $_REQUEST;
        $pUsername = empty($p['username'])?'':trim($p['username']);         
        $pPassword = empty($p['password'])?'':trim($p['password']);         
        $tTime = time();

		if(empty($pUsername)){Tool_Fnc::ajaxMsg('请输入账号');}
		if(empty($pPassword)){Tool_Fnc::ajaxMsg('请输入密码');}
        //if(!Tool_Validate::mo($pPhone)){Tool_Fnc::ajaxMsg('账号不正确');}
		$tAMO = new AdminModel;
        $tARow = $tAMO->field('private_key')->where('name = \''.$pUsername.'\' and role = 7')->fRow();
		$tAkey = empty($tARow['private_key'])?'':$tARow['private_key'];

        $tMd5str = Tool_Fnc::markmd5($pPassword,$tAkey); 
		//$tARow = $tAMO->field('id')->where('name = \''.$pUsername.'\' and password = \''.$tMd5str.'\' and role = 7')->fRow();
        $tSql = 'select id from '.$tAMO->table.' where name = \''.$pUsername.'\' and passwd = \''.$tMd5str.'\' and role = 7 and status = 1';
        $tRow = $tAMO->query($tSql)[0];
        if(empty($tRow['id'])){Tool_Fnc::ajaxMsg("用户名或者密码错误");}


		//添加登陆 记录
		$tData = array(
			'aid' => $tARow['id'],
			'created' => $tTime,
			'ip' => Tool_Fnc::realip(),
		);
        $tSWLLMO = new SupportworkerloginlogModel;

		if(!$tSWLLMO->insert($tData)){
			Tool_Fnc::ajaxMsg("登录失败");
		}
        //查询绑定老人信息 
        $tSMO = new SeniorModel;
        $tWSMO = new WorkerseniorModel;
		$tImgUrl = Yaf_Registry::get("config")->web->url->img;
        $tSql = 'select concat(\''.$tImgUrl.'\',photo) photo,s.id sid,s.name name from '.$tWSMO->table.' ws left join '.$tAMO->table.' a on ws.sw_id = a.id left join '.$tSMO->table.' s on ws.sid = s.id where a.id = '.$tRow['id'].' order by s.id desc';
        $tDatas = $tSMO->query($tSql);
        $tTokenarr=$tAMO->creattoken($tRow['id']);
        $tTokenarr['senior'] = $tDatas;
        Tool_Fnc::ajaxMsg("登录成功",1,$tTokenarr);//登录成功返回token和老人信息
	}
}

